<template>
  <div class="app-container" v-infinite-scroll="getNoticeInfinite" :infinite-scroll-disabled="Infinite">
    <div class="notice-header" style="padding: 20px;">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="通知时间">
          <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
            range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <!-- <el-button type="primary" plain icon="el-icon-plus" size="mini" v-hasPermi="['client:announcement:add']"
                  @click="handleAdd">发布通知</el-button> -->
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getNoticeList" />
      </el-row>
    </div>

    <div v-if="noticeList.length != 0" :data="noticeList" class="notice-list-body">
      <div v-for="(item, index) in noticeList" :key="index">
        <div v-if="item.status == 1" @mouseenter="handleMouseOver(item, index)">
        <!-- <div v-if="item.status == 1"> -->
          <el-card class="notice-list-item">
            <div style="padding: 14px;">
              <h3 class="notice-list-title">{{ "系统通知" }}
                <span class="notice-check-status">
                  {{ parseTime(!!item.updateTime ? item.updateTime : item.createTime,
                    '{y}年{m}月{d}日 {h}:{i}:{s}') }}
                  <span style="color: red;float: right;font-size:16px;" v-if="item.status == 1">New</span>
                </span>
              </h3>
              <div class="notice-list-box" />
              <span class="notice-list-content" v-html="item.noticeContent" />
            </div>
          </el-card>
        </div>
        <div v-else>
          <el-card class="notice-list-item">
            <div style="padding: 14px;">
              <h3 class="notice-list-title">{{ "系统通知" }}
                <span class="notice-check-status">
                  {{ parseTime(!!item.updateTime ? item.updateTime : item.createTime,
                    '{y}年{m}月{d}日 {h}:{i}:{s}') }}
                  <span style="color: red;float: right;font-size:16px;" v-if="item.status == 1">New</span>
                </span>
              </h3>
              <div class="notice-list-box" />
              <span class="notice-list-content" v-html="item.noticeContent" />
            </div>
          </el-card>
        </div>
      </div>

      <el-divider v-if="maxTip" class="bottom-tip">已经到底，没有更多通知了</el-divider>
      <el-backtop target=".notice-list-body" :right="40" :bottom="85" />
    </div>

    <el-empty description="暂无通知" :image-size="250" style="width:100%" v-else />


    <!-- <el-dialog :title="noticeInfo.title" :visible.sync="noticeDrawer" width="700px" :before-close="closeNoticeDrawer"
      class="notice-check-box" @opened="checkLookNotice"> -->
      <el-dialog :title="noticeInfo.title" :visible.sync="noticeDrawer" width="700px" :before-close="closeNoticeDrawer"
      class="notice-check-box" >
      <div class="notice-body">
        <div class="notice-carousel" ref="noticeCarousel" :autoplay="false" arrow="never" :loop="false"
          indicator-position="none">
          <span v-html="noticeInfo.content" class="notice-content" />
        </div>
      </div>
      <span slot="footer" class="notice-footer">
        <span class="notice-seed-time">
          发布于 {{ parseTime(!!noticeInfo.updateTime ? noticeInfo.updateTime : noticeInfo.createTime,
            '{y}年{m}月{d}日 {h}:{i}:{s}') }}
        </span>
        <el-button @click="closeNoticeDrawer" style="margin-left: 20px">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 通知弹窗 -->
  </div>
</template>

<script>
import { listNoticeAll, sureSotificationById } from "@/api/system/notice";

export default {
  name: "noticeList",
  data() {
    return {
      // 滚动分页参数
      InfinitePage: 1,
      // 达到最大页的判断
      maxTip: false,
      // 最大页数
      maxPage: 0,
      // 无限滚动是否可用
      Infinite: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      noticeDrawer: false,
      // 查看详细
      readInfo: false,
      // 通知列表
      noticeList: [],
      noticeInfo: [],
      // 日期范围
      dateRange: [],
      // 通知列表总条数
      noticeTotal: 0,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 查询条件
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: "",
        content: "",
      },
    }
  },
  created() {
    this.getNoticeList();
    sureSotificationById(1).then(response => {});
  },
  methods: {
    handleMouseOver(row, key) {
      this.noticeList[key].status = 2;
      // sureSotificationById(row.id).then(response => {
      // });
    },
    // 获取通知列表
    getNoticeList() {
      this.loading = true;
      let date = {
      }
      if (this.dateRange != null && this.dateRange.length != 0) {
        date = {
          beginTime: this.dateRange[0],
          endTime: this.dateRange[1]
        }
      }
      listNoticeAll(date).then(response => {
        this.noticeList = response.rows;
        if (response.total % 10 != 0) {
          this.maxPage = parseInt(response.total / 10 + 1);
        } else {
          this.maxPage = parseInt(response.total / 10);
        }
        if (this.queryParams.pageNum >= this.maxPage) {
          this.Infinite = true;
          this.maxTip = true;
        } else {
          this.Infinite = false;
          this.maxTip = false;
        }
        this.loading = false;
      });
    },
    // 无限滚动获取数据
    getNoticeInfinite() {
      this.loading = true;
      this.Infinite = true;
      this.InfinitePage += 1;
      this.loading = true;
      listNoticeAll({
        pageNum: this.InfinitePage,
        pageSize: 10,
      }).then(response => {
        this.noticeList = this.noticeList.concat(response.rows);
        this.loading = false;
        if (this.InfinitePage == this.maxPage) {
          this.maxTip = true;
        } else {
          this.Infinite = false;
        }
      })
      this.loading = false;
    },
    // 查询事件
    handleQuery() {
      this.InfinitePage = 1;
      this.queryParams.pageNum = 1;
      this.getNoticeList();
    },
    // 重置查询
    resetQuery() {
      this.dateRange = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 关闭通知弹窗
    closeNoticeDrawer() {
      // 关闭弹窗
      this.noticeDrawer = false;
      setTimeout(() => {
        this.noticeInfo = [];
      }, 300);
    },
    // 打开通知弹窗
    openNoticeDrawer(item) {
      this.noticeInfo = item;
      this.noticeDrawer = true;
    },
    // // 确认查看通知
    // checkLookNotice() {
    //   checkNotice({ id: this.noticeInfo.id }).then(response => {
    //     for (let i = 0; i < this.noticeList.length; i++) {
    //       if (this.noticeList[i].id == this.noticeInfo.id) {
    //         this.noticeList[i].status = 1;
    //         break;
    //       }
    //     }
    //   }).catch(error => { })
    // },
  },
}
</script>

<style lang="scss">
.notice-check-box>.el-dialog>.el-dialog__body {
  padding-top: 0px;
}
</style>

<style lang="scss" scoped>
.app-container {
  padding: 0px;
  overflow: auto;
}

.notice-list-item {
  margin: 0px 40px 40px 40px;
}

.notice-list-item:hover {
  background-color: #d6d6d1;
  border: 1;
}

.notice-list-title {
  margin: 0px;
  margin-bottom: 15px;
}

.button {
  float: right;
}

.notice-check-status {
  margin-left: 10px;
  color: gray;
  font-size: 14px;
}
</style>